package pers.aidenj.ostrich.serviceImpl;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.alibaba.fastjson.JSONObject;

import pers.aidenj.ostrich.common.ResultMap;
import pers.aidenj.ostrich.mapper.AdminInformationMapper;
import pers.aidenj.ostrich.model.AdminInformation;
import pers.aidenj.ostrich.service.LoginService;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Service
public class LoginServiceImpl implements LoginService {


    private static final Logger log = LoggerFactory.getLogger(LoginServiceImpl.class);
    @Resource
    AdminInformationMapper adminInformationMapper;

    @Resource
    ShardedJedisPool shardedJedisPool;

    /**
     * 前台用户登陆
     *
     * @param account
     * @param password
     * @param verifyCode
     * @return
     * @author AidenJ
     * @date 2017年4月7日 16:05:46
     */
    public JSONObject userLogin(String account, String password, String verifyCode) {
        // TODO Auto-generated method stub

        return null;
    }

    /**
     * 后台用户登陆
     *
     * @param account
     * @param password
     * @param verifyCode
     * @param uuid
     * @return
     * @author AidenJ
     * @date 2017年4月7日 16:05:46
     */
    public JSONObject adminLogin(HttpSession httpSession, String account, String password, String verifyCode, String uuid, Model model) {
        // TODO Auto-generated method stub


        JSONObject jsonObject = new JSONObject();


        AdminInformation adminInformation = new AdminInformation();
        adminInformation.setAccount(account);
        adminInformation.setPassword(password);
        //adminInformation.setStatus(0);//用户状态正常
        //adminInformation.setDataState(1);//数据状态正常

        adminInformation = adminInformationMapper.getOneAdmin(adminInformation);
        ShardedJedis jedis = shardedJedisPool.getResource();
        log.info("jedis中存储的参数：" + jedis.get(uuid));
        log.info("前台传递的参数：" + verifyCode);


        //i.
        if (!httpSession.getAttribute(uuid).equals(verifyCode)) {
            jsonObject.put("RESULTDATA", "验证码错误！！！");
            //jedis.expireAt(uuid, LocalDateTime.now().plusMinutes(30L));
            return ResultMap.getFail(jsonObject);
            /**不能返回具体的错误信息，防止恶意扫描账号库**/
        /*}else if(userInformation==null){
			jsonObject.put("RESULTDATA", "此账号不存在！！！");
			return ResultMap.getFail(jsonObject);
		}else if(!password.equals(userInformation.getPassword())){
			jsonObject.put("RESULTDATA", "密码错误，请重新输入！！！");
			return ResultMap.getFail(jsonObject);*/
        } else if (adminInformation == null) {
            jsonObject.put("RESULTDATA", "用户名/密码错误，请重新输入！！！");
            return ResultMap.getFail(jsonObject);
        } else if (adminInformation.getStatus() == 2) {
            jsonObject.put("RESULTDATA", "账号已停用！！！");
            return ResultMap.getFail(jsonObject);
        } else {
            jsonObject.put("RESULTDATA", adminInformation);
            return ResultMap.getSuccess(jsonObject);
        }

    }
}
